import java.util.ArrayList; 
import java.util.Collections;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 * Used to store the list of Products
 * @author sdm21
 *
 */
public class ProductList {
	private ArrayList <Product> productList;
	
	/**
	 * Create a new Product List
	 */
	public ProductList(){
		productList = new ArrayList<Product>();
	}
	
	/**
	 * Finds a product in the list when given the product ID
	 * @param id String of ID number
	 * @return Product with that ID code or null
	 */
	public Product findByID(String id){
		id = id.trim(); 
		/* Loop through all products and return
		 * the product with matching ID number
		 */
		for (Product p : productList){
			if (p.getID().equals(id)){
				return p;
			}
		}
		return null; //Returns null if product not found
	}
	
	/**
	 * Adds product to list unless product already exists.
	 * @param p Product p to add
	 * @return false if the product failed to be added or 
	 * true if the product was successfully added
	 */
	public boolean addProduct(Product p){
		String id = p.getID();
		// tries to find the product in the list
		Product in_list = this.findByID(id);
		if (in_list == null){
		//if no product is returned then p is added
			productList.add(p);
			return true;
		}
		return false;
	}
	
	/**
	 * Prints the details of the products stored in the list
	 */
	public void printProductList(){
		String details = String.format("%-6s", "ID")+String.format("%-10s", "Product")+String.format("%-20s", "Product Name")+String.format("%-5s", "ABV%")
				+String.format("%-14s", "Origin")+String.format("%-36s", "Details")+String.format("%-7s", "£Unit")
				+String.format("%-7s", "£Case")+String.format("%-17s","Quantity in Stock");
		details += "\n\n";
		for (Product p : productList){
			details += p.printDetails();
			details += "\n";
		}
		System.out.println(details);
	}
	
	/**
	 * Divides text input to create product objects
	 * @param line String product info input
	 */
	private void processLine(String line){
		try{
			String parts[] = line.split(",");
			
			String id = parts[0];
			id.trim();
			
			String name = parts[1];
			name.trim();
			
			String string_abv = parts[2];
			string_abv.trim();
			double abv = Double.parseDouble(string_abv);
			
			String country_origin = parts[3];
			country_origin.trim();
			//Wine object
			if (id.charAt(0) == 'W' ){
				String string_year = parts[4];
				string_year.trim();
				int year = Integer.parseInt(string_year);
				
				String grape_variety = parts[5];
				grape_variety = grape_variety.trim();
			
				String wine_type = parts[6];
				wine_type.trim();
				
				String string_unit_price = parts[7];
				string_unit_price.trim();
				double unit_price = Double.parseDouble(string_unit_price);
				
				String string_case_price = parts[8];
				string_case_price.trim();
				double case_price = Double.parseDouble(string_case_price);
				
				String string_quantity = parts[9];
				string_quantity = string_quantity.trim();
				int quantity = Integer.parseInt(string_quantity);
				
				Wines wine = new Wines(id, name, unit_price, case_price, abv, country_origin, year, grape_variety, wine_type, quantity);
				this.addProduct(wine);
			}
			//Beers object
			else if (id.charAt(0) == 'B'){
				String beer_type = parts[4];
				beer_type.trim();
				
				String string_unit_price = parts[5];
				string_unit_price.trim();
				double unit_price = Double.parseDouble(string_unit_price);
				
				String string_case_price = parts[6];
				string_case_price.trim();
				double case_price = Double.parseDouble(string_case_price);
				
				String string_quantity = parts[7];
				string_quantity = string_quantity.trim();
				int quantity = Integer.parseInt(string_quantity);
				
				Beers beer = new Beers(id, name, unit_price, case_price, abv, country_origin, beer_type, quantity);
				this.addProduct(beer);
			}
			//Spirits object
			else if (id.charAt(0) == 'S'){
				String spirit_variety = parts[4];
				spirit_variety.trim();
				
				String string_unit_price = parts[5];
				string_unit_price.trim();
				double unit_price = Double.parseDouble(string_unit_price);
				
				String string_case_price = parts[6];
				string_case_price.trim();
				double case_price = Double.parseDouble(string_case_price);
				
				String string_quantity = parts[7];
				string_quantity = string_quantity.trim();
				int quantity = Integer.parseInt(string_quantity);
				
				Spirits spirit = new Spirits(id, name, unit_price, case_price, abv, country_origin, spirit_variety, quantity);
				this.addProduct(spirit);
			}
			//Liqueurs object
			else if (id.charAt(0) == 'L'){
				String liqueur_type = parts[4];
				liqueur_type.trim();
				
				String string_unit_price = parts[5];
				string_unit_price.trim();
				double unit_price = Double.parseDouble(string_unit_price);
				
				String string_case_price = parts[6];
				string_case_price.trim();
				double case_price = Double.parseDouble(string_case_price);
				
				String string_quantity = parts[7];
				string_quantity = string_quantity.trim();
				int quantity = Integer.parseInt(string_quantity);
				
				
				Liqueurs liqueur = new Liqueurs(id, name, unit_price, case_price, abv, country_origin, liqueur_type, quantity);
				this.addProduct(liqueur);
			}
		}

		catch (NumberFormatException nfe) {
			String error = "Error in CompeitorList.processLine, number conversion error in '"
					+ line + "'  - " + nfe.getMessage();
			System.out.println(error);
		}

		catch (ArrayIndexOutOfBoundsException air) {
			String error = "Error in ProductList.processLine not enough items in  : '" + line
				+ "' index position : " + air.getMessage();
			System.out.println(error);
		}
	}
	
	/**
	 * Loads a file and uses the processLine method to create objects from it
	 * @param filename String of file name
	 */
	public void readFile(String filename) {
		try {
			File f = new File(filename);
			Scanner scanner = new Scanner(f);
			while (scanner.hasNextLine()) {
				String inputLine = scanner.nextLine(); 
				if (inputLine.length() != 0) {
					processLine(inputLine);
				}

			}
		}
		//catch the error of the file name not found
		catch (FileNotFoundException fnf){
			System.out.println("Error in ProductList.readFile "+ filename + " not found.");
			System.exit(0);
		}
	}
	
	/** 
	 * Writes the report to an external text file
	 * @param fileName string name of the file to be written to
	 * @param text the string that will be written to the file
	 */
	public void writeTo(String fileName, String text) {
		FileWriter filewriter;
		try {
			filewriter = new FileWriter(fileName);
			filewriter.write(text);
			filewriter.close();
		}
		//Catch error case where the file name is not found in location
		catch (FileNotFoundException fnf){
			System.out.println(fileName + " not found ");
			System.exit(0);
		}
		//Catch the IOException error
		catch (IOException ioe){
			ioe.printStackTrace();
			System.exit(1);
		}
	}
	
	public void sortName(){
		
	}
	
	public void sortType(){
		
	}
	
	public void sortCountry(){
		
	}
	
	public void sortABV(){
		
	}
	
	public void sortUnitPrice(){
		
	}
	
	public void sortCasePrice(){
		
	}
	
	public void sortQuantity(){
		
	}
}

